class Solution
{
public:
    int minOperations(string s1, string s2, int x)
    {
        if (s1 == s2)
        {
            return 0;
        }
        int n = s1.size();
        vector<int> diffPositions;
        for (int i = 0; i < n; ++i)
        {
            if (s1[i] != s2[i])
            {
                diffPositions.push_back(i);
            }
        }

        int m = diffPositions.size();
        if (m & 1)
        {
            return -1;
        }

        int dp0 = 0;
        int dp1 = x;
        for (int i = 1; i < m; ++i)
        {
            int newDp1 = min(dp0 + 2 * (diffPositions[i] - diffPositions[i - 1]), dp1 + x);
            dp0 = dp1;
            dp1 = newDp1;
        }
        return dp1 / 2;
    }
};